package com.xiyou.week02.map;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 1. 两数之和
 *
 * @ClassName：TwoSum
 * @Author：西柚
 * @Date：2022/1/8 12:00 上午
 * @Versiion：1.0
 */
public class TwoSum {

    /**
     * 输入：nums = [2,7,11,15], target = 9
     * 输出：[0,1]
     * 解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。
     * <p>
     *
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                int[] ints = {map.get(target - nums[i]), i};
                return ints;
            }
            map.put(nums[i], i);
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;

        System.out.println(Arrays.toString(twoSum(nums, target)));
    }
}
